﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

using System.IO.Ports;
namespace PayloadTracker.APRS
{
    public partial class GenericSerialModemSetup : IAPRSProviderSetup
    {
        struct ParserEntry
        {
            public ParserEntry(string name, APRS.Parsers.IParser parser)
            {
                Name = name;
                Parser = parser;
            }
            public string Name;
            public APRS.Parsers.IParser Parser;

            public override string ToString()
            {
                return Name;
            }
        }
        public GenericSerialModemSetup()
        {
            InitializeComponent();



            foreach (string s in SerialPort.GetPortNames())
                //if (!s.Equals("None")) 
                    cbPort.Items.Add(s);
            foreach (string s in Enum.GetNames(typeof(Parity)))
                //if (!s.Equals("None")) 
                    cbParity.Items.Add(s);
            foreach (string s in Enum.GetNames(typeof(StopBits)))
                if(!s.Equals("None"))
                    cbStopBits.Items.Add(s);


            

        }

        public override void Reset()
        {
            if (cbPort.Items.Count == 0)
                return;

            cbBaud.SelectedItem = "9600";
            cbDataBits.SelectedItem = "8";

            cbStopBits.SelectedIndex = cbParity.SelectedIndex = cbStopBits.SelectedIndex = cbPort.SelectedIndex = 0;

            txtInit.Text = string.Empty;
        }
        public override bool ValidateInput()
        {
            if (cbPort.Items.Count == 0)
                return false;

            try
            {
                Int32.Parse(cbBaud.SelectedItem.ToString());
            }
            catch (System.Exception)
            {
                return false;
            }
            return true;
        }
        public override IAPRSProvider CreateInstance()
        {

            if (!ValidateInput())
                throw new Exception("Invalid input");

            APRS.GenericSerialModem.SerialPortConfiguration serial = new GenericSerialModem.SerialPortConfiguration();

            serial.BaudRate = Int32.Parse(cbBaud.SelectedItem.ToString());
            serial.DataBits = Int32.Parse(cbDataBits.SelectedItem.ToString());
            serial.Parity = ((Parity)Enum.Parse(typeof(Parity), cbParity.SelectedItem.ToString()));
            serial.Port = cbPort.SelectedItem.ToString();
            serial.stopBits = ((StopBits)Enum.Parse(typeof(StopBits), cbStopBits.SelectedItem.ToString()));

            APRS.GenericSerialModem.APRSSerialModemConfiguration config = new GenericSerialModem.APRSSerialModemConfiguration();
            config.InitilizationData = txtInit.Text;
            //TODO: Move this into config dialog.
            config.NewLineDeliminator = "\r";
            config.SerialConfiguration = serial;


            

            return new GenericSerialModem(config);
        }

        private void GenericSerialModemSetup_Load(object sender, EventArgs e)
        {
            Reset();
        }
    }
}
